package org.gruposp2p.proyectokarma.gwt.client.ui;

import org.gruposp2p.proyectokarma.gwt.client.MainEntryPoint;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;

public class UploadDocumentDialog {

	interface Style extends CssResource {
    	String errorTextBox();
    	String textBox();
	  }

	private static UploadDocumentDialogUiBinder uiBinder = GWT
			.create(UploadDocumentDialogUiBinder.class);

	interface UploadDocumentDialogUiBinder extends
			UiBinder<Widget, UploadDocumentDialog> {
	}

	@UiField Style style;
	@UiField DialogBox dialogBox;
    @UiField VerticalPanel messagePanel;
    @UiField Label messageLabel;
    @UiField VerticalPanel formDataPanel;
    @UiField FileUpload fileUpload;
    @UiField FormPanel formPanel;
    @UiField HorizontalPanel buttonPanel;
    @UiField Button acceptButton;
    @UiField Button cancelButton;
    @UiField LayoutPanel loadingPanel;

	public UploadDocumentDialog() {
		uiBinder.createAndBindUi(this);
        acceptButton.setStylePrimaryName("acceptButton");
        cancelButton.setStylePrimaryName("cancelButton");
		loadingPanel.setVisible(false);
        messagePanel.setVisible(false);
		formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		formPanel.setMethod(FormPanel.METHOD_POST);
		formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
		   public void onSubmitComplete(SubmitCompleteEvent event) {
			   dialogBox.hide();
		    }
		});
	}

	public void show() {
		messagePanel.setVisible(false);
		formPanel.setVisible(true);
		buttonPanel.setVisible(true);
		loadingPanel.setVisible(false);
		dialogBox.center();
		dialogBox.show();
	}

	private boolean isValidForm () {
		Log.debug("Filename: '" + fileUpload.getFilename() + "'");
		if (fileUpload.getFilename() == null
				|| "".equals(fileUpload.getFilename())) {
			setMessage(MainEntryPoint.INSTANCE.constants.emptyFieldException());
			fileUpload.setStyleName(style.errorTextBox(), true);
			return false;
		} else {
			fileUpload.setStyleName(style.errorTextBox(), false);
		}
		return true;
	}

	private void setMessage (String message) {
    	messageLabel.setText(message);
    	messagePanel.setVisible(true);
	}
	
    @UiHandler("acceptButton")
    void handleAcceptButton(ClickEvent e) {
		if (!isValidForm()) return;
		formPanel.submit();
		loadingPanel.setVisible(true);
		formPanel.setVisible(false);
		buttonPanel.setVisible(false);
    }
	
    @UiHandler("cancelButton")
    void handleCancelButton(ClickEvent e) {
    	dialogBox.hide();
    }

}